//
// Created by ZLY on 2025/5/12.
//

#include "scene_title.h"

#include <fstream>

#include "cmath"
#include "scene_main.h"
#include "screen/hud_button.h"
#include "screen/hud_text.h"
#include "screen/ui_mouse.h"

void SceneTitle::renderBackground() {
    game_.drawBoundry(glm::vec2(30), game_.getScreenSize() - glm::vec2(30), 10.0f, boundary_color_);
}

void SceneTitle::init() {
    Scene::init();
    loadData("resources/score.dat");
    // SDL_ShowCursor();
    SDL_HideCursor();
    game_.playMusic("resources/bgm/Spooky music.mp3");
    auto size = glm::vec2(game_.getScreenSize().x / 2.0f, game_.getScreenSize().y / 3.0f);
    HUDText::addHUDTextChild(this, "幽 灵 逃 生", size, game_.getScreenSize() / 2.0f - glm::vec2(0, 100),
                             "resources/font/VonwaonBitmap-16px.ttf", 64);
    auto score_text = "最高分： " + std::to_string(game_.getHighScore());
    HUDText::addHUDTextChild(this, score_text, glm::vec2(200, 50), game_.getScreenSize() / 2.0f + glm::vec2(0, 100),
                             "resources/font/VonwaonBitmap-16px.ttf", 32);

    button_exit_ = HUDButton::addHUDButtonChild(this, game_.getScreenSize() / 2.0f + glm::vec2(200),
                                                "resources/UI/A_Quit1.png",
                                                "resources/UI/A_Quit2.png",
                                                "resources/UI/A_Quit3.png", 2.0f, Anchor::CENTER);
    button_start_ = HUDButton::addHUDButtonChild(this, game_.getScreenSize() / 2.0f + glm::vec2(-200, 200),
                                                 "resources/UI/A_Start1.png",
                                                 "resources/UI/A_Start2.png",
                                                 "resources/UI/A_Start3.png", 2.0f, Anchor::CENTER);
    button_credits_ = HUDButton::addHUDButtonChild(this, game_.getScreenSize() / 2.0f + glm::vec2(0, 200),
                                                   "resources/UI/A_Credits1.png",
                                                   "resources/UI/A_Credits2.png",
                                                   "resources/UI/A_Credits3.png",
                                                   2.0f, Anchor::CENTER);
    auto text = game_.loadTextFile("resources/credits.txt");
    credits_text_ = HUDText::addHUDTextChild(this, text, glm::vec2(600, 600), game_.getScreenSize() / 2.0f);
    credits_text_->setBgSizeByText(50);
    credits_text_->setActive(false);
    mouse_ = UIMouse::addUIMouseChild(this, "resources/UI/pointer_c_shaded.png", "resources/UI/pointer_c_shaded.png",
                                      1.0f, Anchor::TOP_LEFT);
}

void SceneTitle::render() {
    Scene::render();
    renderBackground();
}

void SceneTitle::update(float dt) {
    updateColor();
    color_timer_ += dt;
    if (credits_text_->getActive()) {
        mouse_->update(dt);
        return;
    }
    Scene::update(dt);
    checkButtonQuit();
    checkButtonStart();
    checkButtonCredits();
}

void SceneTitle::clean() {
    Scene::clean();
    game_.stopMusic();
}

bool SceneTitle::handleEvents(SDL_Event &event) {
    if (credits_text_->getActive()) {
        if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
            credits_text_->setActive(false);
        }
        return true;
    }
    Scene::handleEvents(event);
    return false;
}

void SceneTitle::loadData(const std::string &file_path) {
    Scene::loadData(file_path);
    int high_score = 0;
    std::ifstream file(file_path, std::ios::binary);
    if (file.is_open()) {
        file.read(reinterpret_cast<char *>(&high_score), sizeof(int));
        file.close();
    }
    game_.setHighScore(high_score);
}

void SceneTitle::updateColor() {
    boundary_color_.r = 0.5f + 0.5f * sinf(color_timer_ * 0.9f);
    boundary_color_.g = 0.5f + 0.5f * sinf(color_timer_ * 0.8f);
    boundary_color_.b = 0.5f + 0.5f * sinf(color_timer_ * 0.7f);
}

void SceneTitle::checkButtonQuit() {
    if (button_exit_->isTrigger()) {
        game_.quit();
    }
}

void SceneTitle::checkButtonCredits() {
    if (button_credits_->isTrigger()) {
        credits_text_->setActive(true);
        button_credits_->setIsTrigger(false);
    }
}

void SceneTitle::checkButtonStart() {
    if (button_start_->isTrigger()) {
        auto scene = new SceneMain();
        game_.safeChangeScene(scene);
    }
}
